<?php
namespace App\Http\Controllers\Admin\Point;

use App\Service\Admin\Point\GoodsService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class GoodsController extends BaseController
{
    //商品

    public function __construct(GoodsService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //分类
    public function getCateList()
    {
        $res = $this->db->getCateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //一个
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //添加
    public function addOne(Request $request)
    {
        $pas['title'] = $request->post('title', '');
        $pas['cate_id'] = $request->post('cateid', 0);
        $pas['bigimg'] = $request->post('bigImg', '');
        $pas['isnew'] = $request->post('isnew', 0);
        $pas['detail'] = $request->post('detail', '');
        $moreImgs = $request->post('moreImgs', []);
        $skulist = $request->post('skulist', []);
        if (!$pas['title']) {
            return $this->toJson(-2, '商品名称必须的');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '商品分类必须的');
        }
        if (!$pas['bigimg']) {
            return $this->toJson(-2, '主图必须的');
        }
        if (!count($moreImgs)) {
            return $this->toJson(-2, '详情图必须的');
        }
        if (!count($skulist)) {
            return $this->toJson(-2, 'SKU规格必须的');
        }
        $res = $this->db->addOne($pas, $moreImgs, $skulist);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //详情图上传
    public function moreUpload(Request $request)
    {
        $id = $request->post('id', 0);
        $img = $request->post('img', '');
        if (!$id) {
            return $this->toJson(-2, '商品ID必须的');
        }
        if (!$img) {
            return $this->toJson(-2, '商品图片必须的');
        }
        $moreImg = Upload::getPathByBase64($img);
        $imgInfo = getimagesize($img);
        $res = $this->db->moreUpload($id, $moreImg, $imgInfo[0], $imgInfo[1]);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //详情图删除
    public function moreDel(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = $this->db->moreDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['cate_id'] = $request->post('cateid', 0);
        $pas['bigimg'] = $request->post('bigImg', '');
        $pas['isnew'] = $request->post('isnew', 0);
        $pas['detail'] = $request->post('detail', '');
        if (!$id) {
            return $this->toJson(-2, '商品ID必须的');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '商品名称必须的');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '商品分类必须的');
        }
        $res = $this->db->modify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function setDel(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //SKU添加
    public function skuAdd(Request $request)
    {
        $pas['gid'] = $request->post('gid', 0);
        $pas['title'] = $request->post('title', '');
        $pas['price'] = $request->post('price', 0);
        $pas['price_line'] = $request->post('price_line', 0);
        $pas['stock'] = $request->post('stock', 0);
        if (!$pas['gid']) {
            return $this->toJson(-2, '商品ID必须的');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '规格名称必须的');
        }
        if (!$pas['price']) {
            return $this->toJson(-2, '单价必须的');
        }
        if (!$pas['price_line']) {
            return $this->toJson(-2, '划线价必须的');
        }
        if (!$pas['stock']) {
            return $this->toJson(-2, '库存必须的');
        }
        $res = $this->db->skuAdd($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //SKU修改
    public function skuModify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['price'] = $request->post('price', 0);
        $pas['price_line'] = $request->post('price_line', 0);
        $pas['stock'] = $request->post('stock', 0);
        if (!$id) {
            return $this->toJson(-2, 'SKU的ID必须的');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '规格名称必须的');
        }
        if (!$pas['price']) {
            return $this->toJson(-2, '单价必须的');
        }
        if (!$pas['price_line']) {
            return $this->toJson(-2, '划线价必须的');
        }
        if (!$pas['stock']) {
            return $this->toJson(-2, '库存必须的');
        }
        $res = $this->db->skuModify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //SKU删除
    public function skuDel(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'SKU的ID必须的');
        }
        $res = $this->db->skuDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}